package com.sgb.kjwl.view.ui.adapter.home;


import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.view.ui.fragment.home.LazyFragment2;
import com.sgb.kjwl.view.ui.fragment.home.TitleItemFragment1;
import com.sgb.kjwl.view.ui.fragment.home.TitleItemFragment2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * 首页商品界面的中部的布局
 */
public class TitleItemAdapter extends FragmentStateAdapter {

    private List<LazyFragment2> fragmentList = new ArrayList<>();
    private List<MenuEntity> menuEntities;

    public TitleItemAdapter(@NonNull FragmentActivity fragmentActivity, List<LazyFragment2> fragmentList1) {
        super(fragmentActivity);
        this.fragmentList = fragmentList1;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getItemCount() {
        return fragmentList.size();
    }

    public List<LazyFragment2> getFragmentList() {
        return fragmentList;
    }


    public List<MenuEntity> getMenuEntities() {
        return menuEntities;
    }

    public void setData(List<MenuEntity> menuEntityList) {
        if (menuEntityList != null && menuEntityList.size() != 0) {
            if (menuEntities == null || menuEntities.size() == 0) {
                menuEntities = menuEntityList;
            } else {
                menuEntities.clear();
                menuEntities.addAll(menuEntityList);
            }
            refreshItemMenu(menuEntityList);
        }
    }

    /***
     * 更新数据
     *
     * @param menuEntityList
     */
    private void refreshItemMenu(List<MenuEntity> menuEntityList) {
        if (fragmentList.size() > 0) {
            for (LazyFragment2 fragment : fragmentList) {
                if (fragment instanceof TitleItemFragment1) {
                    TitleItemFragment1 fragment1 = (TitleItemFragment1) fragment;
                    fragment1.loadData(menuEntityList);
                } else if (fragment instanceof TitleItemFragment2) {
                    TitleItemFragment2 fragment2 = (TitleItemFragment2) fragment;
                    fragment2.loadData(menuEntityList);
                }
            }
        }
    }

    public interface OnItemClick<T> {
        /***
         * item被点击
         * @param t
         * @param position
         */
        void onItemClick(T t, int position);
    }

    /***
     * item点击事件
     * @param onItemClick
     */
    public void setOnItemClick(OnItemClick<MenuEntity> onItemClick) {
        if (fragmentList != null && fragmentList.size() > 0) {
            for (LazyFragment2 fragment : fragmentList) {
                if (fragment instanceof TitleItemFragment1) {
                    TitleItemFragment1 fragment1 = (TitleItemFragment1) fragment;
                    fragment1.setOnItemClick(onItemClick);
                } else if (fragment instanceof TitleItemFragment2) {
                    TitleItemFragment2 fragment2 = (TitleItemFragment2) fragment;
                    fragment2.setOnItemClick(onItemClick);
                }
            }
        }

    }

}
